/**
 * 路由配置文件（超级清晰版）
 * 
 * @file frontend/src/routes/index-clear.tsx
 * @description 最清晰易读的路由配置，使用配置对象 + 类型安全
 * @author 类小脑计算平台开发团队
 * @date 2025-11-15
 * 
 * 特点：
 * 1. 使用配置数组，一目了然
 * 2. 保持完整的类型安全
 * 3. 自动生成路由对象
 * 4. 支持中文注释说明
 */

import React, { lazy, Suspense, ComponentType } from 'react';
import { RouteObject, Navigate } from 'react-router-dom';

// ============================================================================
// 类型定义
// ============================================================================

/**
 * 路由配置项
 */
interface RouteConfig {
  /** 路由路径 */
  path: string;
  /** 组件路径（相对于 src 目录） */
  component: string;
  /** 路由描述（中文） */
  description: string;
  /** 对应的设计图编号（可选） */
  frame?: number;
}

/**
 * 重定向配置项
 */
interface RedirectConfig {
  /** 源路径 */
  from: string;
  /** 目标路径 */
  to: string;
  /** 重定向原因 */
  reason: string;
}

// ============================================================================
// 辅助组件
// ============================================================================

/** 加载中组件 */
const LoadingFallback: React.FC = () => (
  <div style={{
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    height: '100vh',
    fontSize: '18px',
    color: '#666'
  }}>
    加载中...
  </div>
);

// ============================================================================
// 路由配置数据
// ============================================================================

/**
 * 主要页面路由配置
 */
const MAIN_ROUTES: RouteConfig[] = [
  // ========== 首页和主配置 ==========
  {
    path: '/',
    component: 'pages/Welcome',
    description: '欢迎页面 - 应用首页',
  },
  {
    path: '/cell-config',
    component: 'pages/Main',
    description: '细胞类型配置页面',
    frame: 8,
  },
  {
    path: '/netpyne',
    component: 'components/NetPyNE/NetPyNE',
    description: 'NetPyNE 核心组件',
  },

  // ========== 模型定义 ==========
  {
    path: '/populations',
    component: 'pages/Populations',
    description: '种群配置',
  },
  {
    path: '/syn-mechs',
    component: 'pages/SynMechs',
    description: '突触机制配置',
  },
  {
    path: '/conn-rules',
    component: 'pages/ConnRules',
    description: '连接规则配置',
  },
  {
    path: '/subcell-conn-rules',
    component: 'pages/SubcellConnRules',
    description: '亚细胞连接规则',
  },
  {
    path: '/stim-sources',
    component: 'pages/StimSources',
    description: '刺激源配置',
  },
  {
    path: '/stim-targets',
    component: 'pages/StimTargets',
    description: '刺激目标配置',
  },

  // ========== 仿真配置 ==========
  {
    path: '/simulation-config-general',
    component: 'pages/SimulationConfigGeneral',
    description: '仿真配置 - 通用设置',
    frame: 10,
  },
  {
    path: '/simulation-config-record',
    component: 'pages/SimulationConfigRecord',
    description: '仿真配置 - 记录设置',
    frame: 18,
  },
  {
    path: '/simulation-config-network',
    component: 'pages/SimulationConfigNetwork',
    description: '仿真配置 - 网络设置',
    frame: 19,
  },
  {
    path: '/simulation-config-network-v2',
    component: 'pages/SimulationConfigNetworkV2',
    description: '仿真配置 - 网络设置 V2',
    frame: 21,
  },
  {
    path: '/simulation-config-network-editor',
    component: 'pages/SimulationConfigNetworkEditor',
    description: '仿真配置 - 网络编辑器',
    frame: 24,
  },
  {
    path: '/simulation-config-save',
    component: 'pages/SimulationConfigSave',
    description: '仿真配置 - 保存设置',
    frame: 20,
  },
  {
    path: '/simulation-config-save-v2',
    component: 'pages/SimulationConfigSaveV2',
    description: '仿真配置 - 保存设置 V2',
    frame: 22,
  },

  // ========== 绘图设置 ==========
  {
    path: '/plot-settings-raster',
    component: 'pages/PlotSettingsRaster',
    description: '绘图设置 - 光栅图',
    frame: 13,
  },
  {
    path: '/plot-settings-trajectory',
    component: 'pages/PlotSettingsTrajectory',
    description: '绘图设置 - 轨迹图',
    frame: 14,
  },
  {
    path: '/plot-settings-lfp',
    component: 'pages/PlotSettingsLFP',
    description: '绘图设置 - 局部场电位',
    frame: 16,
  },

  // ========== 反应扩散 ==========
  {
    path: '/reaction-diffusion-region',
    component: 'pages/ReactionDiffusionRegion',
    description: '反应扩散 - 区域设置',
    frame: 15,
  },
  {
    path: '/reaction-diffusion-empty',
    component: 'pages/ReactionDiffusionEmpty',
    description: '反应扩散 - 空白页面',
    frame: 17,
  },

  // ========== 导入功能 ==========
  {
    path: '/import-neuroml',
    component: 'pages/ImportNeuroML',
    description: '导入 NeuroML 文件',
    frame: 23,
  },
  {
    path: '/import-python',
    component: 'pages/ImportPythonDialog',
    description: '导入 Python 脚本',
    frame: 31,
  },

  // ========== 实验管理 ==========
  {
    path: '/experiment-manager',
    component: 'pages/ExperimentManager',
    description: '实验管理器',
    frame: 25,
  },

  // ========== 对话框 ==========
  {
    path: '/select-folder',
    component: 'pages/SelectFolderDialog',
    description: '选择文件夹对话框',
    frame: 26,
  },
  {
    path: '/open-json',
    component: 'pages/OpenJSONDialog',
    description: '打开 JSON 文件对话框',
    frame: 27,
  },
  {
    path: '/save-json',
    component: 'pages/SaveJSONDialog',
    description: '保存 JSON 文件对话框',
    frame: 28,
  },
  {
    path: '/save-json-options',
    component: 'pages/SaveJSONDialogOptions',
    description: '保存 JSON 选项对话框',
    frame: 30,
  },
  {
    path: '/delete-confirm',
    component: 'pages/DeleteConfirmDialog',
    description: '删除确认对话框',
    frame: 29,
  },

  // ========== 特殊页面 ==========
  {
    path: '/geppetto-test',
    component: 'pages/GeppettoTest',
    description: 'Geppetto WebSocket 测试页面',
  },
];

/**
 * 兼容性路由配置（重定向）
 */
const LEGACY_REDIRECTS: RedirectConfig[] = [
  {
    from: '/synaptic-mechanisms',
    to: '/syn-mechs',
    reason: '旧路径重定向到新路径',
  },
  {
    from: '/connectivity-rules',
    to: '/conn-rules',
    reason: '旧路径重定向到新路径',
  },
  {
    from: '/subcellular-connectivity',
    to: '/subcell-conn-rules',
    reason: '旧路径重定向到新路径',
  },
  {
    from: '/stimulation-sources',
    to: '/stim-sources',
    reason: '旧路径重定向到新路径',
  },
  {
    from: '/stimulation-targets',
    to: '/stim-targets',
    reason: '旧路径重定向到新路径',
  },
  {
    from: '/plot-settings-traces',
    to: '/plot-settings-trajectory',
    reason: '旧名称重定向到新名称',
  },
  {
    from: '/reaction-diffusion-extracellular',
    to: '/reaction-diffusion-empty',
    reason: '旧名称重定向到新名称',
  },
];

/**
 * Frame 路由配置（设计图编号重定向）
 */
const FRAME_REDIRECTS: RedirectConfig[] = [
  { from: '/frame/8', to: '/cell-config', reason: 'Frame 8 -> 细胞配置' },
  { from: '/frame/10', to: '/simulation-config-general', reason: 'Frame 10 -> 仿真通用配置' },
  { from: '/frame/13', to: '/plot-settings-raster', reason: 'Frame 13 -> 光栅图设置' },
  { from: '/frame/14', to: '/plot-settings-trajectory', reason: 'Frame 14 -> 轨迹图设置' },
  { from: '/frame/15', to: '/reaction-diffusion-region', reason: 'Frame 15 -> 反应扩散区域' },
  { from: '/frame/16', to: '/plot-settings-lfp', reason: 'Frame 16 -> LFP 设置' },
  { from: '/frame/17', to: '/reaction-diffusion-empty', reason: 'Frame 17 -> 反应扩散空白' },
  { from: '/frame/18', to: '/simulation-config-record', reason: 'Frame 18 -> 仿真记录配置' },
  { from: '/frame/19', to: '/simulation-config-network', reason: 'Frame 19 -> 仿真网络配置' },
  { from: '/frame/20', to: '/simulation-config-save', reason: 'Frame 20 -> 仿真保存配置' },
  { from: '/frame/21', to: '/simulation-config-network-v2', reason: 'Frame 21 -> 仿真网络配置 V2' },
  { from: '/frame/22', to: '/simulation-config-save-v2', reason: 'Frame 22 -> 仿真保存配置 V2' },
  { from: '/frame/23', to: '/import-neuroml', reason: 'Frame 23 -> 导入 NeuroML' },
  { from: '/frame/24', to: '/simulation-config-network-editor', reason: 'Frame 24 -> 网络编辑器' },
  { from: '/frame/25', to: '/experiment-manager', reason: 'Frame 25 -> 实验管理器' },
  { from: '/frame/26', to: '/select-folder', reason: 'Frame 26 -> 选择文件夹' },
  { from: '/frame/27', to: '/open-json', reason: 'Frame 27 -> 打开 JSON' },
  { from: '/frame/28', to: '/save-json', reason: 'Frame 28 -> 保存 JSON' },
  { from: '/frame/29', to: '/delete-confirm', reason: 'Frame 29 -> 删除确认' },
  { from: '/frame/30', to: '/save-json-options', reason: 'Frame 30 -> 保存 JSON 选项' },
  { from: '/frame/31', to: '/import-python', reason: 'Frame 31 -> 导入 Python' },
];

// ============================================================================
// 路由生成函数
// ============================================================================

/**
 * 组件缓存（避免重复创建）
 */
const componentCache = new Map<string, React.LazyExoticComponent<ComponentType<any>>>();

/**
 * 获取懒加载组件（带缓存）
 */
function getLazyComponent(componentPath: string): React.LazyExoticComponent<ComponentType<any>> {
  if (!componentCache.has(componentPath)) {
    componentCache.set(
      componentPath,
      lazy(() => import(`../${componentPath}`))
    );
  }
  return componentCache.get(componentPath)!;
}

/**
 * 根据配置生成路由对象
 */
function createRouteFromConfig(config: RouteConfig): RouteObject {
  const Component = getLazyComponent(config.component);
  
  return {
    path: config.path,
    element: (
      <Suspense fallback={<LoadingFallback />}>
        <Component />
      </Suspense>
    ),
  };
}

/**
 * 根据重定向配置生成路由对象
 */
function createRedirectFromConfig(config: RedirectConfig): RouteObject {
  return {
    path: config.from,
    element: <Navigate to={config.to} replace />,
  };
}

// ============================================================================
// 生成路由
// ============================================================================

/** 主要页面路由 */
const mainRoutes: RouteObject[] = MAIN_ROUTES.map(createRouteFromConfig);

/** 兼容性路由 */
const legacyRoutes: RouteObject[] = LEGACY_REDIRECTS.map(createRedirectFromConfig);

/** Frame 路由 */
const frameRoutes: RouteObject[] = FRAME_REDIRECTS.map(createRedirectFromConfig);

// ============================================================================
// 导出路由配置
// ============================================================================

/**
 * 应用的完整路由配置
 * 
 * 路由统计：
 * - 主要页面路由：30 个
 * - 兼容性路由：7 个
 * - Frame 路由：21 个
 * - 总计：58 个路由
 */
const routes: RouteObject[] = [
  ...mainRoutes,
  ...legacyRoutes,
  ...frameRoutes,
];

export default routes;

// 导出配置数据（用于文档生成等）
export {
  MAIN_ROUTES,
  LEGACY_REDIRECTS,
  FRAME_REDIRECTS,
  mainRoutes,
  legacyRoutes,
  frameRoutes,
};

